//
//  ImageProcessViewController.m
//  BaseTools
//
//  Created by 李挺哲 on 15/8/12.
//  Copyright (c) 2015年 ltz. All rights reserved.
//

#import "ImageProcessViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "Toast+UIView.h"
#import "BaseImageCropperViewController.h"
#import "UIImage+UIImage_Helper.h"
@interface ImageProcessViewController ()<VPImageCropperDelegate>

@end

@implementation ImageProcessViewController


/**
*  通过imageview的layer来操作
*
*  @param ImageView <#ImageView description#>
*/
- (void)getRoundImageView :(UIImageView *) ImageView{
    [ImageView.layer setCornerRadius:(ImageView.frame.size.height/2)];
    [ImageView.layer setMasksToBounds:YES];
    [ImageView setContentMode:UIViewContentModeScaleAspectFill];
    [ImageView setClipsToBounds:YES];
    ImageView.layer.shadowColor = [UIColor blackColor].CGColor;
    ImageView.layer.shadowOffset = CGSizeMake(4, 4);
    ImageView.layer.shadowOpacity = 0.5;
    ImageView.layer.shadowRadius = 2.0;
    ImageView.layer.borderColor = [[UIColor blackColor] CGColor];
    ImageView.layer.borderWidth = 2.0f;
    ImageView.userInteractionEnabled = YES;
    ImageView.backgroundColor = [UIColor blackColor];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNavigationTitle:@"图片处理" barTintColor:0x1E86CB titleColor:0xFFFFFF isTranslucent:NO isHidden:NO];
    [self showNavigationLeftButtonTitle:@"返回" tintColor:0xFFFFFF];
    UIImage *baseImage = [UIImage imageNamed:@"image"];
    self.imageView.image = baseImage;
//  
//    [self getRoundImageView :_roundImageView];
//    _roundImageView.image=baseImage;
    
    UIImage * newImage =[UIImage clipRoundWithImageName:@"image" bordersW:3 borderColor:[UIColor redColor]];
    _roundImageView.image=newImage;
    UIImage *blurredImage = [baseImage blurredImage:0.2f];
    self.blurImageView.image = blurredImage;
    
    
    UIImage * buttonImage=[UIImage resizedImage:@"toolbar_light_comment@2x"];
    [_button1 setBackgroundImage:buttonImage forState:UIControlStateNormal];

    
    UIImage * sbuttonImage=[UIImage resizedImage:@"toolbar_light_comment@2x"left:0.5 top:0.2];
     [_button2 setBackgroundImage:sbuttonImage forState:UIControlStateNormal];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)toRoundImageAction:(id)sender {
    [self getRoundImageView :_imageView];
    
}



- (IBAction)selectPictureAction:(id)sender {
    [self presentCameraVC];
    
    
}


- (IBAction)viewConvertImage:(id)sender {
    
   UIImage *image= [UIImage viewConvertTOImage:self.view];
    
    _tempImageView.image=image;
    
}

- (IBAction)savePhoto:(id)sender {
    if( self.imageView.image )
    {
        ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
        [library writeImageToSavedPhotosAlbum:self.imageView.image.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
        
            [self.view makeToast:[NSString stringWithFormat:@"图片保存在%@",assetURL]];
            
        }];
        
       
    }
}


- (IBAction)screenshotAction:(id)sender {
    
    _screenshotImageView.image=[UIImage screenshot];
    
    
    
}

-(void)selectImageFinished:(NSData *)image{
        UIImage * selectImage=[UIImage imageWithData:image];
    BaseImageCropperViewController *imgEditorVC = [[BaseImageCropperViewController alloc] initWithImage:selectImage cropFrame:CGRectMake(0, 100.0f, self.view.frame.size.width, self.view.frame.size.width) limitScaleRatio:3.0];
    imgEditorVC.delegate = self;
    [self presentViewController:imgEditorVC animated:YES completion:^{
        // TO DO
    }];


}
//#pragma mark VPImageCropperDelegate
- (void)imageCropper:(BaseImageCropperViewController *)cropperViewController didFinished:(UIImage *)editedImage {
   
    [cropperViewController dismissViewControllerAnimated:YES completion:^{
       
        _roundImageView.image=editedImage;
        _blurImageView.image=[editedImage blurredImage:0.2f];
    }];
}

- (void)imageCropperDidCancel:(BaseImageCropperViewController *)cropperViewController {
    [cropperViewController dismissViewControllerAnimated:YES completion:^{
    }];
}
- (IBAction)blurImage:(UIButton *)sender {
    [_imageView setImage:[_imageView.image blurredImage:0.2f]];
    
    
}

@end
